package com.isunland.managesystem.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.FunctionObject;
import com.isunland.managesystem.entity.GuideMenu;
import com.isunland.managesystem.utils.ConfigUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/8 09:47
 */
public class GridSecondViewAdapter extends BaseAdapter {
  private Context context;
  private ArrayList<GuideMenu> mList;
  private ImageLoader mImageLoader;

  public GridSecondViewAdapter(Context context, ArrayList<GuideMenu> list) {
    this.context = context;
    this.mList = list;
    this.mImageLoader = RequestManager.getImageLoader();
  }

  @Override public int getCount() {
    return mList.size();
  }

  @Override public Object getItem(int i) {
    return mList.get(i);
  }

  @Override public long getItemId(int i) {
    return i;
  }

  @TargetApi(Build.VERSION_CODES.M) @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null) {
      view = LayoutInflater.from(context).inflate(R.layout.gridview_second_item, null);
    }
    GuideMenu item = mList.get(i);

    LinearLayout llGuideName = (LinearLayout) view.findViewById(R.id.ll_item);
    TextView tvGuideName = (TextView) view.findViewById(R.id.tv_guideName);
    TextView tvHintNumber = (TextView) view.findViewById(R.id.tv_hintNumber);
    ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
    //图标显示
    //changeIcon(item.getAlias(), ivIcon);
    String icon = item.getDefaultUrl();
    if (!TextUtils.isEmpty(icon)) {
      if (MyStringUtil.contains(icon, "?")) {
        icon = MyStringUtil.substring(icon, 0, icon.indexOf("?"));
      }
      String urlImage = ApiConst.getDownLoadUrlByGet(icon);
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivIcon, // 控件名
          R.drawable.icon_liuchengrenwu, R.drawable.icon_liuchengrenwu); // 默认图标
      mImageLoader.get(urlImage, listener);
    } else {
      ivIcon.setImageResource(R.drawable.icon_liuchengrenwu);
    }

    tvGuideName.setText(item.getResName());
    //动态创建圆角
    GradientDrawable gd = new GradientDrawable();//创建drawable
    //noinspection deprecation
    gd.setColor(context.getResources().getColor(R.color.white));//内部填充颜色
    gd.setCornerRadius(0);//圆角半径
    //noinspection deprecation
    gd.setStroke(2, context.getResources().getColor(R.color.grid_stroke));//边框宽度,颜色
    //noinspection deprecation
    llGuideName.setBackgroundDrawable(gd);
    //显示提示数量
    FunctionObject config = ConfigUtil.get(context, MyStringUtil.valueOfInt(item.getAlias(), 0));
    tvHintNumber.setVisibility(
        (config == null || config.getHintNumber() == 0) ? View.GONE : View.VISIBLE);
    tvHintNumber.setText((config == null) ? "" : String.valueOf(config.getHintNumber()));
    //HintNumber mHintNumber = HintNumber.newInstance(context);
    //remindNumber(mHintNumber, item.getAlias(), tvHintNumber);
    return view;
  }

  //private void remindNumber(HintNumber hintNumber, String alias, TextView tvHintNumber) {
  //  if ("105".equalsIgnoreCase(alias) && hintNumber.getmScoreNumber() != 0) {
  //    //奖惩积分审批
  //    tvHintNumber.setVisibility(View.VISIBLE);
  //    tvHintNumber.setText(String.valueOf(hintNumber.getmScoreNumber()));
  //  } else if ("33".equalsIgnoreCase(alias) && (hintNumber.getmPersonPlanNumber() != 0
  //      || hintNumber.getmDeptPlanNumber() != 0)) {
  //    //个人和部门计划审批: 个人+部门
  //    int sunNumber = hintNumber.getmPersonPlanNumber() + hintNumber.getmDeptPlanNumber();
  //    tvHintNumber.setVisibility(View.VISIBLE);
  //    tvHintNumber.setText(String.valueOf(sunNumber));
  //  } else {
  //    tvHintNumber.setVisibility(View.GONE);
  //  }
  //}

  //@SuppressWarnings("unused") private void changeIcon(String alias, ImageView ivIcon) {
  //  if ("2001".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.appointment_order);//呼叫下单
  //  } else if ("2002".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.agricultural_machinery);//农机
  //  } else if ("2003".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.agricultural_technology);//农技
  //  } else if ("2004".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.knowledge_base);//农业知识库
  //  } else if ("2010".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.problem_back);//问题反馈
  //  } else if ("2011".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.locale_work);//作业任务
  //  } else if ("2012".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.online_receive);//接单
  //  } else if ("2013".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.work_area);//地块库
  //  } else if ("2014".equalsIgnoreCase(alias)) {
  //    ivIcon.setImageResource(R.drawable.pay_money);//支付中心
  //  } else {
  //    ivIcon.setImageResource(R.drawable.icon_jihuarenwu);//默认图标
  //  }
  //}

  @Override public boolean isEmpty() {
    return true;
  }
}

